package com.itheima.health.service.impl;


import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void selectAll(List<OrderSetting> orderSettings) {

    }

    /*
    根据日期范围进行查询
     */
    @Override
    public List<OrderSetting> getOrderSettingByMonth(int year, int month) {
      //设置日期的范围   format是用来完成Date对象转换为String对象 可以实现日期对象的转换
        String startDate = String.format("%d-%d-1",year,month);
        String endDate = String.format("%d-%d-31",year,month);
        //查询dao层  并返回
       return orderSettingDao.selectByOrderDate1(startDate,endDate);
    }



    /*
    文件进行批量添加
     */

    @Override
    public void add(List<OrderSetting> orderSettings) {
        //进行批量添加数据
        for (OrderSetting orderSetting : orderSettings) {
            orderSettingDao.insert(orderSetting);
        }
    }
    /*
   根据预约日期进行人数的修改
     */
    @Transactional
    @Override
    public void updateByOrderId(int number, Date orderDate) {
        //把日期查询出来 进行判断 看看日期是否存在
       OrderSetting dateId  =  orderSettingDao.selectOrderDate(orderDate);
       log.info("查询出来的id是{}",dateId);
       if (dateId != null){ //判断日期是否存在
           //存在就进行更新
           orderSettingDao.updateByDateId(dateId.getId(),number);
       }else {
           //不存在就做添加
           orderSettingDao.insert(new OrderSetting(orderDate,number));
       }
    }
}
